﻿using AutoMapper;
using B.S.DataBase.Myproject.Helper;
using B.S.DataBase.MyProject.Domain.ArrivalCheck;
using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.Domain.ViewModels;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using System;
using T.E.ERP.Read.API.Applicantions.Command.CheckCommand;
using static Humanizer.In;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.CheckCommandHandler
{
    public class GetArrivalCheckListCommandHandler:IRequestHandler<GetArrivalCheckListCommand,APIResult<List<View_ArrivalCheckList>>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<ArrivalCheck> arrivalCheckBiz;

        public GetArrivalCheckListCommandHandler(IMapper mapper, IBaseRepository<ArrivalCheck> arrivalCheckBiz)
        {
            this.mapper = mapper;
            this.arrivalCheckBiz = arrivalCheckBiz;
        }

        public Task<APIResult<List<View_ArrivalCheckList>>> Handle(GetArrivalCheckListCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<View_ArrivalCheckList>> result = new APIResult<List<View_ArrivalCheckList>>();
            var checkList = arrivalCheckBiz.GetAll().Result;
            if (request.Id.ToString().IsNotNull())
            {
                checkList = checkList.Where(x => x.Id == request.Id);
            }
            result.Count = checkList.Count();
            result.data = checkList.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).OrderByDescending(c => c.CreateTime)
                .Select(c => new View_ArrivalCheckList
                {
                    Id = c.Id,
                    Theme = c.Theme,
                    SourceId = c.SourceId,
                    MaterialsName = c.MaterialsName,
                    CheckStatus = c.CheckStatus,
                    CheckDempt = c.CheckDempt,
                    CheckUser = c.CheckUser,
                    Remark = c.Remark,
                    CreateUser = c.CreateUser,
                    CreateTime = c.CreateTime.DateTimeToString("yyyy-MM-dd HH:mm:ss", "--"),
                    UpdateUser = c.UpdateUser,
                    UpdateTime = c.UpdateTime.DateTimeToString("yyyy-MM-dd HH:mm:ss", "--"),

                }).ToList();
            return Task.FromResult(result);
        }
    }
}
